package com.dhcc.bpm.modules.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.alarm.entity.Alarm;
import com.dhcc.bpm.modules.alarm.entity.AlarmStrategy;
import com.dhcc.bpm.modules.alarm.service.IAlarmService;
import com.dhcc.bpm.modules.alarm.service.IAlarmStrategyService;
import com.dhcc.bpm.modules.alarm.vo.AlarmStrategyVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AlarmStrategyController
 * @Description 告警策略表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-alarm/alarmstrategy")
@Api(value = "告警策略表", tags = "告警策略表接口")
public class AlarmStrategyController extends BladeController {

	private IAlarmStrategyService alarmStrategyService;

	private IAlarmService alarmService;

	private ISerialNumberService serialNumberService;

	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 17:32
	 * @param alarmStrategy
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.alarm.entity.AlarmStrategy>
	 **/
	@ApiLog("告警策略详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.SELECT)
	public R<AlarmStrategy> detail(AlarmStrategy alarmStrategy) {
		AlarmStrategy detail = alarmStrategyService.getOne(Condition.getQueryWrapper(alarmStrategy));
		return R.data(detail);
	}

	/**
	 * @Description 分页 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:32
	 * @param alarmStrategy
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.alarm.entity.AlarmStrategy>>
	 **/
	@ApiLog("告警策略分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.SELECT)
	public R<IPage<AlarmStrategy>> list(@ApiIgnore @RequestParam Map<String, Object> alarmStrategy, Query query) {
		IPage<AlarmStrategy> pages = alarmStrategyService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmStrategy,AlarmStrategy.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:32
	 * @param alarmStrategy
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.alarm.vo.AlarmStrategyVO>>
	 **/
	@ApiLog("告警策略分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.SELECT)
	public R<IPage<AlarmStrategyVO>> page(AlarmStrategyVO alarmStrategy, Query query) {
		IPage<AlarmStrategyVO> pages = alarmStrategyService.selectAlarmStrategyPage(Condition.getPage(query), alarmStrategy);
		return R.data(pages);
	}

	/**
	 * @Description 新增 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:32
	 * @param alarmStrategy
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警策略新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody AlarmStrategy alarmStrategy) {
		return R.status(alarmStrategyService.save(alarmStrategy));
	}

	/**
	 * @Description 修改 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:32
	 * @param alarmStrategy
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警策略修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody AlarmStrategy alarmStrategy) {
		return R.status(alarmStrategyService.updateById(alarmStrategy));
	}

	/**
	 * @Description 新增或修改 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:33
	 * @param alarmStrategy
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警策略新增修改")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入alarmStrategy")
	@Log(title = "告警策略表",businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody AlarmStrategy alarmStrategy) {
		if (alarmStrategy.getId() == null){
		    //设置策略编号
            String code = serialNumberService.generateSerialNumberByModelCode("opc_alarm_strategy");
            alarmStrategy.setStrategyCode(code);
        }

		String alarmThreshold = alarmStrategy.getAlarmThreshold();
		switch (alarmThreshold){
			case "1":   //alarmThreshold 1:流量，
				alarmStrategy.setSuccessRatio(0L);
				alarmStrategy.setErrorCode("");
				break;
			case "2":   //2：失败率
				alarmStrategy.setAlarmFlow(0L);
				alarmStrategy.setErrorCode("");
				break;
			case "3":     //3：错误码
				alarmStrategy.setAlarmFlow(0L);
				alarmStrategy.setSuccessRatio(0L);
				break;
			default:
				break;
		}
//		String content = "";
//		if(alarmStrategy.getAlarmThreshold().equals("1")){ // 流量告警策略
//			content = "流量预警提示：开放平台接口${param0}流量达到${param1}%,请及时注意查看与调整";
//		}else if(alarmStrategy.getAlarmThreshold().equals("2")){ // 失败率告警策略
//			content = "失败率告警提示：开放平台接口${param0}失败率超过${param1}%,请及时注意查看与调整";
//		}else if(alarmStrategy.getAlarmThreshold().equals("3")){ // 错误码告警策略
//			content = "错误码告警提示：开放平台接口${param0}错误码出现${param1},请及时注意查看与调整";
//		}
//		alarmStrategy.setAlarmContentMould(content);
		return R.status(alarmStrategyService.saveOrUpdate(alarmStrategy));
	}

	
	/**
	 * @Description 删除 告警策略表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:33
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警策略删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "告警策略表",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Map<String ,Object> map = new HashMap<>();
		List<Long> arryid =  Func.toLongList(ids);
		map.put("array",arryid.toArray());
		List<Alarm> details = alarmService.selectByIds(map);
		if(details != null && details.size() > 0){
			return R.fail(201, "数据正在使用中，暂时无法删除！");
		}
		return R.status(alarmStrategyService.deleteLogic(Func.toLongList(ids)));
	}

	@ApiLog("查询策略")
	@Log(title = "查询策略",businessType = BusinessType.SELECT)
	@GetMapping("/selectAlarmStrategyNameById")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "查询策略", notes = "id")
	public JSONObject selectAlarmStrategyNameById(@ApiParam(value = "主键", required = true) @RequestParam String id) {
		Alarm alarm = alarmService.getById(id);
		JSONObject object = new JSONObject();
		object.put("success",true);
		object.put("names",alarmStrategyService.selectAlarmStrategyNameById(alarm.getOpcAlarmStrategyId()).get(0));
		return object;
	}


}
